import type { UserInfo } from '@vben/types';

import type { AttachmentApi } from '../core/attachment';

import type Api from '#/typings/common';

import { requestClient } from '#/api/request';

export namespace UserApi {
  export interface UserInfoResult {
    id: number;
    accountId?: null | number;
    signPassWord: string;
    category: 0 | 1 | 2 | null;
    adminType: 0 | 1 | 2 | null;
    name: string;
    nickName: string;
    unitId: null | number;
    unitName: null | string;
    gender: null | number;
    birthDay: Date | null;
    telePhone: string;
    email: string;
    sortNum: null | number;
    status: null | number;
    description: null | string;
    headIcon: AttachmentApi.AttachmentDto | null;
    roles: Api.CommonDtoRecord[];
    roleIds: number[];
  }

  export interface UserListResult extends Api.Common.CommonRecord {
    id: number;
    accountId?: null | number;
    signPassWord: string;
    category: 0 | 1 | 2 | null;
    adminType: 0 | 1 | 2 | null;
    name: string;
    nickName: string;
    unitId: null | number;
    unitName: null | string;
    gender: null | number;
    birthDay: Date | null;
    telePhone: string;
    email: string;
    sortNum: null | number;
    status: null | number;
    description: null | string;
    headIcon: AttachmentApi.AttachmentDto | null;
    roleNames: string;
    roleIds: string;
  }

  export interface AddUser {
    name: string;
    nickName: string;
    unitId: null | number;
    unitName: null | string;
    passWord: string;
    gender: null | number;
    birthDay: Date | null;
    telePhone: string;
    email: string;
    sortNum: null | number;
    status: null | number;
    description: null | string;
    headIcon: AttachmentApi.AttachmentDto | null;
    adminType: 0 | 1 | 2 | null;
    roleIds: number[];
  }
}

/**
 * 获取用户信息
 */
async function getUserInfoApi() {
  return requestClient.get<UserInfo>('sys/user/info');
}

async function getUserPageList(params: Api.Common.PageParams) {
  return requestClient.get<Array<UserApi.UserListResult>>('sys/user/list', {
    params,
  });
}

async function addUser(addUserInput: UserApi.AddUser) {
  return requestClient.post('sys/user', addUserInput);
}

async function editUser(id: number, editUserInput: UserApi.AddUser) {
  return requestClient.put(`sys/user/${id}`, editUserInput);
}

async function deleteUser(id: number) {
  return requestClient.delete(`sys/user/${id}`);
}

export { addUser, deleteUser, editUser, getUserInfoApi, getUserPageList };
